import 'dart:io';
import 'dart:isolate';

import '../error/result.dart';
import 'interface.dart';
import 'interpreter.dart';

/**
 *   PACKAGE:
 *   USER:Administrator
 *   TIME:2021/1/3 0003 11:13
 *   Description:
 */
ReceivePort rp1;//线程1-接收端
SendPort sp1;//线程1-发送端
Isolate isolate1;//线程1
void doWork(SendPort x){
  print("start work iso1");
  sp1 = x;
  rp1 = new ReceivePort();
  rp1.listen(listenMain);
  sp1.send(Result.iso(1,"rp1.sendPort",rp1.sendPort));

  isolate1 = Isolate.current;
  isolate1.addErrorListener(sp1);
  isolate1.addOnExitListener(sp1,response:"exit ok");
}

/**
 * 处理主线程发过来的消息
 */
void listenMain(result){
  if(result != null && result is Result) {
    print("[main send data]:${result.toString()}");
    switch(result.CODE){
      case 1:
         interpreter_compile(result.DATA);
         var t1 = interpreter_run(null,Interpreter.getInstance().mainModule,Interpreter.getInstance()
             .module[Interpreter.getInstance().mainModule].statement_list);
         sp1.send(Result.iso(2,t1.error,null));
         sp1.send(Result.iso(0,"iso1 run over",Isolate.current));
        break;
      default:
    }
  }else{
    print("[main send error]:${result.toString()}");
  }
}




